package com.example.flow

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
    private val mainViewModel:MainViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
    }

    fun start(view: View) {
        lifecycleScope.launch {
            //页面处于start状态接受数据，最标准的写法
            repeatOnLifecycle(Lifecycle.State.STARTED){
                mainViewModel.timeFlow1.collectLatest{//解决背压
                    Log.d(TAG, "timeFlow1: $it")
                    delay(3000)
                }

            }



//            //子协程
//            launch {
//                mainViewModel.timeFlow2.collect{
//                    Log.d(TAG, "timeFlow2: $it")
//                }
//            }
        }

    }
}